package ext.tianma.part.RawMaterial.builder;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.iba.definition.StringDefinition;
import wt.iba.value.ReferenceValue;
import wt.iba.value.StringValue;
import wt.log4j.LogR;
import wt.part.WTPart;
import wt.query.ClassAttribute;
import wt.query.OrderBy;
import wt.query.QuerySpec;
import wt.query.SearchCondition;
import wt.type.ClientTypedUtility;
import wt.type.TypeDefinitionForeignKey;
import wt.type.TypeDefinitionReference;
import wt.type.Typed;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;

import com.ptc.jca.mvc.components.JcaTableConfig;
import com.ptc.mvc.components.AbstractComponentBuilder;
import com.ptc.mvc.components.ColumnConfig;
import com.ptc.mvc.components.ComponentBuilder;
import com.ptc.mvc.components.ComponentConfig;
import com.ptc.mvc.components.ComponentConfigFactory;
import com.ptc.mvc.components.ComponentParams;

import ext.tianma.bom.util.EnumerationUtil;
import ext.tianma.part.report.searchAPL.util.APLUtil;

@ComponentBuilder({ "ext.tianma.part.RawMaterial.builder.SearchRawMaterialTableBuilder" })
public class SearchRawMaterialTableBuilder extends AbstractComponentBuilder {

	private static final Logger logger = LogR
			.getLogger(SearchRawMaterialTableBuilder.class.getName());

	@Override
	public Object buildComponentData(ComponentConfig config,
			ComponentParams params) throws Exception {
		List<Object> obj = new ArrayList<Object>();
		String materialType = (String) params.getParameter("MaterialType");//材料型号
		String classId = (String) params.getParameter("classId");//二级分类
		if (StringUtils.isEmpty(materialType) && StringUtils.isEmpty(classId)) {
			return obj;
		}
		return getCustomer(classId, materialType);
	}

	public static String getSqllike(String param) {
		if (StringUtils.isBlank(param)) {
			return "%";
		}

		return param.replaceAll("\\*", "%");

	}

	/**
	 * <功能详细描述>
	 * 
	 * @param name
	 * @return
	 * @throws Exception 
	 * @throws WTPropertyVetoException
	 * @see [类、类#方法、类#成员]
	 */
	public static List<?> getCustomer(String classId, String materialType) throws Exception {
		Hashtable<String, Object> strMap = new Hashtable<String, Object>();
		strMap.put("MaterialType", materialType);//材料型号
		List<WTPart> list = queryAPLPart(classId, strMap);
		return list;
	}
	
	/**
	 * 获取APL材料
	 * @param usableRange 使用范围
	 * @param state 生命周期状态
	 * @return
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2020年11月23日
	 */
	public static List<WTPart> queryAPLPart(String classId, Hashtable<String, Object> strMap) throws Exception {
		List<WTPart> parts = new ArrayList<WTPart>();
		
		QuerySpec qs = new QuerySpec();
	    int p = qs.appendClassList(WTPart.class, true);
	    qs.setAdvancedQueryEnabled(true);
	    
	    //仅查询APL材料
		TypeDefinitionReference clientType = ClientTypedUtility.getTypeDefinitionReference("cn.tianma.AVLMaterial");
		SearchCondition sc = new SearchCondition(WTPart.class,
				Typed.TYPE_DEFINITION_REFERENCE + "." + TypeDefinitionReference.KEY + "." + TypeDefinitionForeignKey.BRANCH_ID, 
				SearchCondition.EQUAL, clientType.getKey().getBranchId());
		qs.appendWhere(sc, new int[] { 0 });
		
		//最新小版本
		qs.appendAnd();
		SearchCondition sc1 = new SearchCondition(WTPart.class,"iterationInfo.latest", SearchCondition.IS_TRUE, true);
		qs.appendWhere(sc1, new int[] { 0 });
		
		//最新大版本
		//addSearchConditionLasterVersion(qs, WTPart.class,null);
		
		if(null != strMap && strMap.size() > 0) {
			APLUtil.addIBAStringCondition(WTPart.class, qs, strMap, SearchCondition.LIKE, "", true, StringDefinition.class, StringValue.class);
		}
		
		//分类id
		if(StringUtils.isNotEmpty(classId)){
			  //select * from pdm.referencevalue r where r.ida3b5=所选分类id and r.ida3a4 = partida2a2; 
			Long nodeId =  Long.valueOf(classId);
			int r = qs.appendClassList(ReferenceValue.class, false);
			SearchCondition s1 = new SearchCondition(ReferenceValue.class,"theIBAReferenceableReference.key.id", SearchCondition.EQUAL, nodeId);
			SearchCondition s2 = new SearchCondition(ReferenceValue.class,"theIBAHolderReference.key.id",
					WTPart.class, "thePersistInfo.theObjectIdentifier.id");
			
			qs.appendAnd();
			qs.appendWhere(s1,new int[]{ r });
			
			qs.appendAnd();
			qs.appendWhere(s2,new int[]{ r, p });
		}
		
		qs.appendOrderBy(new OrderBy(new ClassAttribute(WTPart.class, "thePersistInfo.modifyStamp"), true), new int[]{p});
	    QueryResult qr = PersistenceHelper.manager.find(qs);
		
	    while(qr.hasMoreElements()){
	    	Object o[] = (Object[])qr.nextElement();
  	      	if(o[0] instanceof WTPart){
				WTPart part = (WTPart) o[0];
				parts.add(part);
			}
  	    }
		return parts;
	}

	@Override
	public ComponentConfig buildComponentConfig(ComponentParams params)
			throws WTException {
		ComponentConfigFactory factory = getComponentConfigFactory();
		JcaTableConfig table = (JcaTableConfig) factory.newTableConfig();
		table.setSingleSelect(true);
		table.setSelectable(true);
//		String key = (String) params.getParameter("usableRange");
		Map<String,String> levels = EnumerationUtil.getEnumerationValues("Level"); //等级枚举
		table.setFindInTableEnabled(false);//在表格中搜索框

		ColumnConfig icon = factory.newColumnConfig("type_icon", true);
		icon.setLabel("");
		table.addComponent(icon);

		ColumnConfig col = factory.newColumnConfig("number", true);
		col.setLabel("编码");
		col.setWidth(100);
		col.setSortable(true);
		table.addComponent(col);
		
		col =factory.newColumnConfig("name",true);
		col.setLabel("名称");
		col.setWidth(100);
		col.setSortable(true);
		table.addComponent(col);

		col = factory.newColumnConfig("MaterialType", true);
		col.setLabel("材料型号");
		col.setWidth(100);
		col.setSortable(true);
		table.addComponent(col);
		
		col = factory.newColumnConfig("releaseSite", true);
		col.setLabel("release产地");
		col.setWidth(100);
		col.setSortable(true);
		table.addComponent(col);
		
//		col = factory.newColumnConfig("UsableRange1", true);
//		col.setLabel(levels.get("UsableRange1"));
//		col.setWidth(100);
//		col.setSortable(true);
//		table.addComponent(col);
//		
//		col = factory.newColumnConfig("UsableRange2", true);
//		col.setLabel(levels.get("UsableRange2"));
//		col.setWidth(100);
//		col.setSortable(true);
//		table.addComponent(col);
//		
//		col = factory.newColumnConfig("UsableRange3", true);
//		col.setLabel(levels.get("UsableRange3"));
//		col.setWidth(100);
//		col.setSortable(true);
//		table.addComponent(col);
//		
//		col = factory.newColumnConfig("UsableRange4", true);
//		col.setLabel(levels.get("UsableRange4"));
//		col.setWidth(100);
//		col.setSortable(true);
//		table.addComponent(col);
//		
//		col = factory.newColumnConfig("UsableRange5", true);
//		col.setLabel(levels.get("UsableRange5"));
//		col.setWidth(100);
//		col.setSortable(true);
//		table.addComponent(col);
//		
//		col = factory.newColumnConfig("UsableRange6", true);
//		col.setLabel(levels.get("UsableRange6"));
//		col.setWidth(100);
//		col.setSortable(true);
//		table.addComponent(col);
//		
//		col = factory.newColumnConfig("UsableRange7", true);
//		col.setLabel(levels.get("UsableRange7"));
//		col.setWidth(100);
//		col.setSortable(true);
//		table.addComponent(col);
//		
//		col = factory.newColumnConfig("UsableRange8", true);
//		col.setLabel(levels.get("UsableRange8"));
//		col.setWidth(100);
//		col.setSortable(true);
//		table.addComponent(col);

		return table;
	}

}
